package com.pda.yuanjing.pages.pron.add;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.R;
import com.pda.yuanjing.database.DataBaseControl;
import com.pda.yuanjing.database.RelationRecord;
import com.pda.yuanjing.dialog.product.ProductDialog;
import com.public_module.basemvp.activity.BaseActivity_noMVP;
import com.public_module.tools.DateUtils;
import com.public_module.tools.SharedPreferencesManger;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BillAddActivity extends BaseActivity_noMVP {
    private String TAG = "BillAddActivity";

    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.tvbill)
    EditText tvbill;
    @BindView(R.id.product)
    TextView product;
    @BindView(R.id.edcode)
    EditText edcode;
    @BindView(R.id.logine)
    Button logine;
    @BindView(R.id.bzbl)
    TextView bzbl;

    ProductDialog productDialog;
    String pstr = "";
    int cnum = 0;

    protected void initdata() {
        tvTitle.setText("离线生产建单");
        tvbill.setText("SC" + DateUtils.stampToDateTime(new Date().getTime()));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addprobill);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick({R.id.iv_title_left, R.id.tv_right, R.id.product})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_right:
                if (tvbill.getText().toString().length() == 0) {
                    showMsg("请填写单号");
                    return;
                }
                if (edcode.getText().toString().length() == 0) {
                    showMsg("请填写数量");
                    return;
                }
                if (Integer.parseInt(edcode.getText().toString()) <= 0) {
                    showMsg("应扫数必须大于0");
                    return;
                }
                if (pstr.length() == 0) {
                    showMsg("请填写产品");
                    return;
                }
                int total = Integer.parseInt(edcode.getText().toString());
                if (total % cnum != 0) {
                    showMsg("应扫数必须是所选产品规格的整数倍");
                    return;
                }
                RelationRecord relationRecord = new RelationRecord();
                relationRecord.setBillID(tvbill.getText().toString());
                relationRecord.setBillCode(tvbill.getText().toString());
                relationRecord.setCreateTime(DateUtils.stampToDate(new Date().getTime()));
                relationRecord.setDesStoreID(SharedPreferencesManger.getStoreID());
                relationRecord.setDesStoreName(SharedPreferencesManger.getStoreName());
                relationRecord.setProductID(pstr);
                relationRecord.setProductName(product.getText().toString());
                relationRecord.setCaseBoxNum(cnum);
                relationRecord.setYnum(Integer.parseInt(edcode.getText().toString()));
                relationRecord.setSnum(0);
                relationRecord.setBillListID(pstr + "-" + DateUtils.stampToDateTime(new Date().getTime()));
                DataBaseControl.addRelationBill(relationRecord);
                showMsg("添加订单成功");
                finish();
                break;
            case R.id.product:
                productDialog = new ProductDialog(mContext, R.style.CommentDialog, new ProductDialog.PriorityListener() {
                    @Override
                    public void setActivityText(String str, String str1, int num) {
                        product.setText(str);
                        pstr = str1;
                        cnum = num;
                        bzbl.setText("0/"+cnum);
                        productDialog.dismiss();
                    }
                });
                WindowManager.LayoutParams lp = productDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                productDialog.show();
                break;
        }
    }
}
